<script setup>
import { useFileSelect } from "@/composables/fileSelect.js";
import { watch } from "vue";
import { ElMessage } from "element-plus";
import { useCreate } from "@/store/useCreate.js";

const props = defineProps({
  img: {
    type: String,
    default: "",
  },
  imgIndex: {
    type: Number,
    default: 0,
  },
  imgList: {
    type: Array,
    default: () => [],
  },
});

const createStore = useCreate();
const { selectedFile, triggerFileSelect, uploadFile } = useFileSelect();

watch(selectedFile, (newVal) => {
  console.log(newVal);
  fileUpload();
});

const fileUpload = async () => {
  try {
    const res = await uploadFile();
    if (res.code === 200) {
      const introduce = createStore.formData.introduce.introduce.split(",");
      console.log(introduce);
      introduce[props.imgIndex] = res.data.url;
      createStore.formData.introduce.introduce = introduce.join(",");
      console.log(createStore.formData.introduce.introduce);
    } else {
      ElMessage.error("图片上传失败，请重试！");
    }
  } catch (e) {
    ElMessage.error("图片上传失败，请重试！");
  }
};

const del = () => {
  createStore.formData.introduce.introduce =
    createStore.formData.introduce.introduce
      .split(",")
      .filter((item, index) => index !== props.imgIndex)
      .join(",");
};
const imgMoveUp = () => {
  const introduce = createStore.formData.introduce.introduce.split(",");
  const temp = introduce[props.imgIndex];
  introduce[props.imgIndex] = introduce[props.imgIndex - 1];
  introduce[props.imgIndex - 1] = temp;
  createStore.formData.introduce.introduce = introduce.join(",");
};
const imgMoveDown = () => {
  const introduce = createStore.formData.introduce.introduce.split(",");
  const temp = introduce[props.imgIndex];
  introduce[props.imgIndex] = introduce[props.imgIndex + 1];
  introduce[props.imgIndex + 1] = temp;
  createStore.formData.introduce.introduce = introduce.join(",");
};
</script>

<template>
  <div class="img-box">
    <div @click="triggerFileSelect">
      <div
        v-if="props.img"
        class="img-bg"
        :style="{ backgroundImage: `url(${props.img})` }"
      ></div>
      <span class="add">+ {{ props.img ? "修改" : "添加" }}图片</span>
    </div>
    <div class="tool" v-if="props.imgIndex !== 0">
      <img class="more-icon" src="@/assets/img-more-tool.png" alt="" />
      <div class="tools">
        <span
          ><img src="@/assets/more-select-2.png" alt="" @click="del"
        /></span>
        <span><img src="@/assets/img-up.png" alt="" @click="imgMoveUp" /></span>
        <span
          ><img
            src="@/assets/img-down.png"
            alt=""
            @click="imgMoveDown"
            v-if="props.imgIndex < props.imgList.length - 1"
        /></span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.img-box {
  width: 490px;
  height: 110px;
  border-radius: 16px 16px 16px 16px;
  background: #cacccf;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  position: relative;
  .img-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 16px 16px 16px 16px;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
  }
  .add {
    background: rgba(255, 255, 255, 0.12);
    border-radius: 8px 8px 8px 8px;
    border: 1px solid #ffffff;
    width: 108px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    position: relative;
    z-index: 1;
    cursor: pointer;
    user-select: none;
  }
}
.tool {
  position: absolute;
  top: 12px;
  left: -24px;
  &:hover {
    .tools {
      display: flex;
    }
  }
  .more-icon {
    width: 20px;
    height: 20px;
    cursor: pointer;
  }
  .tools {
    display: none;
    position: absolute;
    top: 0;
    left: -56px;
    z-index: 2;
    align-items: center;
    flex-direction: column;
    box-shadow: 0px 0px 16px 0px rgba(11, 13, 18, 0.06);
    border-radius: 6px 6px 6px 6px;
    border: 1px solid rgba(25, 30, 40, 0.08);
    width: 48px;
    padding: 3px 0;
    background: #fff;
    &:after {
      content: "";
      position: absolute;
      display: block;
      top: 0;
      right: -50px;
      width: 50px;
      height: 100%;
    }
    span {
      width: 40px;
      height: 44px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 6px 6px 6px 6px;
      cursor: pointer;
      &:hover {
        background: #f8f7f7;
      }
      img {
        width: 16px;
        height: 16px;
      }
    }
  }
}
</style>